﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItem.h"
#include "GnufInfo.h"
#include "GnufTextControl.h"

namespace gnuf {
    class Toolbar;
    class Block;
    class NameValuePair;
    class QtText;
    class Sprite;

    class GNUF_BASE_EXPORT Text final
        : public ItemWith<QtText>
        , public TextControlAdpatation
    {
        CANNOT_COPY_OR_ASSIGN(Text);
        GNUF_WATCHABLE_OBJECT(Text)
    public:
        static const EItemType Type;
        explicit Text(Item *);
        virtual ~Text();

        // Note by fanxc：在Table中使用Text时如需要记录文字行为，开启日志记录功能，使用完记得关闭
        // QtText的默认行为会在QtText层拦截日志记录功能，为了使LineEdit承接日志功能，需要这里开启和关闭
        static void turnOnJournalRecord();
        static void turnOffJournalRecord();

        void setText(const std::wstring &, bool notify = false);
        const std::wstring & text() const;

        void setMaxLength(int maxLength);

        void setValidator(const std::wstring& validator);
        const std::wstring& validator() const { return m_validator; }

        ///\brief Set an error text.
        void setError(const std::wstring & text);
        ///\breif Clear the error text.
        void clearError();
        ///\brief Set an sprite.
        void setSprite(const Sprite &);
        ///\brief Clear the sprite.
        void clearSprite();
        ///\return Return the sprite.
        const Sprite * sprite() const { return m_opSprite.get(); }

        void enableTooltip(bool val) { m_enableTooltip = val; }
        bool isEnableTooltip() const { return m_enableTooltip; }

        void setReadonly(bool);
        bool isReadOnly() const { return m_readonly; }

    protected:
        virtual QtItem* getQtItem() const override;
        // Inherits from ItemWith...
        virtual void onDisconnectFromMyQtItem() override;
        virtual void onPostVisibilityChanged() override;
        virtual void onPostAbilityChanged() override;
        virtual void onPostAppearanceSettingChanged() override;
        virtual void onTableRowSelectionChanged(bool) override;

        // Inherits from TextControlAdpatation...
        virtual void onTextChanged(const std::wstring &) override;
        virtual void onAbilityChanged(bool) override;
        virtual void onCollectLocation(std::set<const gcmp::UniqueString *> &) const override;

    private:
        gcmp::OwnerPtr<Info> m_info;
        bool m_readonly;
        bool m_enableTooltip;
        bool m_workwrap;
        std::wstring m_validator;
        gcmp::OwnerPtr< Sprite > m_opSprite;
    };
    
}
